package logic

import (
	"context"
	"time"
	"go-zero-service/service/user/model"
	"go-zero-service/service/user/api/internal/svc"
	"go-zero-service/service/user/api/internal/types"
	"go-zero-service/common/tool"
	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginReply, err error) {
	// todo: add your logic here and delete this line
	var info model.User
	result := l.svcCtx.UserModel.Where("mobile = ?", req.Mobile).Find(&info)

	ps := tool.CheckPassword(req.Password, info.Password)
	s := false
	t := ""

	if ps {
		s = true
		t, err = tool.GetJwtToken(l.svcCtx.Config.Auth.AccessSecret, time.Now().Unix(), l.svcCtx.Config.Auth.AccessExpire, info.Uuid)
	}

	return &types.LoginReply{
		Status:  s,
		Token: t,
	}, result.Error
}
